package com.ccl.iot.object;

import com.ccl.iot.object.IOTObject;

public class IOTInfoWithName implements IOTObject {
	private long iID;
	private String iName;
	
	public IOTInfoWithName(){
	}
	
	public IOTInfoWithName(long aID, String aName){
		iID = aID;
		iName = aName;
	}
	
	public IOTInfoWithName(IOTInfoWithName aInfo){
		if (aInfo != null){
			iID = aInfo.iID;
			iName = aInfo.iName;
		}
	}

	@Override
	public long getID() {
		return iID;
	}
	
	public void setID(long aID){
		iID = aID;
	}
	
	public String getName(){
		return iName;
	}
	
	public boolean setName(String aName){
		if (aName != iName){
			iName = aName;
			
			return true;
		}else{
			return false;
		}
	}
	
	public boolean setName(byte aBuffer[], int aPos, int aLength){
		if (aBuffer != null && aLength > 0 && aBuffer.length >= aPos + aLength){
			String tName = (new String(aBuffer, 0, aLength)).trim();
			
			if (tName.equals(iName) == false){
				iName = tName;
				
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public IOTInfoWithName clone() {
		return new IOTInfoWithName(this);
	}

	@Override
	public String toString() {
		return iID + ":" + iName;
	}
	
	public int compareIgnoreID(IOTInfoWithName aInfo){
		if (aInfo != null){
			if (iName != null){
				return iName.compareTo(aInfo.iName) == 0 ? 0 : 1;
			}else{
				return aInfo.getName() == null ? 0 : 1;
			}
		}
		
		return -1;
	}
}
